<template>
<div class="loginPage">
  <JdNavbar title='购物车' rightText='注册' @skip='register'></JdNavbar>
  <van-form @submit="onSubmit">
    <van-field
      v-model="username"
      name="用户名"
      label="用户名"
      placeholder="用户名"
      :rules="[{ required: true, message: '请填写用户名' }]"
    />
    <van-field
      v-model="password"
      type="password"
      name="密码"
      label="密码"
      placeholder="密码"
      :rules="[{ required: true, message: '请填写密码' }]"
    />
    <div style="margin: 16px;">
      <van-button round block type="info" native-type="submit">登录</van-button>
    </div>
  </van-form>
</div>
</template>

<script>
import {
  JdNavbar
} from '@/components'

export default {
  name: '',
  data() {
    return {
      username: '',
      password: '',
    }
  },

  components: {
    JdNavbar,
  },

  methods: {
    onSubmit() {
      const data = {
        username: this.username,
        password: this.password,
        password2: this.password2
      }
      // 判断用户是否登录成功，登录成功后，后端返回一个token值
      this.$api.isLogin(data).then(res => {
        if (res.token) {
          this.$dialog.success({
            title: 'success',
            message: '登录成功',
            theme: 'round-button'
          }).then(() => {
            localStorage.setItem('token', res.token)
            this.$router.back()
          })
        }
      })
    },

    register () {
      this.$router.push('/register')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>